package com.rec.smile.rpc.api.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import com.rec.smile.rpc.api.util.SerializationUtil;

public class RpcEncoder extends MessageToByteEncoder<Object> {

	
	private Class<?> genericClass;
	
	public RpcEncoder(Class<?> genericClass) {
		super();
		this.genericClass = genericClass;
	}


	@Override
	protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out)
			throws Exception {
		if(genericClass.isInstance(msg)){
			byte[] data = SerializationUtil.serialize(msg);
			out.writeInt(data.length);
			out.writeBytes(data);
		}
	}

}
